<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="850px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form
                ref="formRef"
                label-width="120px"
                :model="formData"
                :rules="formRules"
                :inline="true"
            >
                <!-- <el-form
        ref="formRef"
        :model="formData"
        label-width="84px"
        :rules="formRules"
      > -->
                <el-form-item label="案件类型" prop="caseType">
                    <el-select
                        class="w-[200px]"
                        v-model="formData.caseType"
                        placeholder="请选择案件类型"
                    >
                        <el-option label="专利" value="1" />
                        <el-option label="商标" value="2" />
                        <el-option label="版权" value="3" />
                        <el-option label="科技服务" value="4" />
                        <el-option label="法律案件" value="5" />
                        <el-option label="调查案" value="6" />
                    </el-select>
                </el-form-item>
                <el-form-item label="处理事项" prop="handlingInformation">
                    <el-select
                        class="w-[200px]"
                        v-model="formData.handlingInformation"
                        placeholder="请输入处理事项"
                    >
                        <el-option label="开卷" value="1" />
                        <el-option label="检索分析" value="2" />
                        <el-option label="新申请" value="3" />
                        <el-option label="发客户审核" value="4" />
                        <el-option label="取得申请号" value="5" />
                        <el-option label="定稿提前IPR" value="6" />
                    </el-select>
                </el-form-item>
                <el-form-item label="案件阶段" prop="caseStage">
                    <el-input
                        v-model="formData.caseStage"
                        class="w-[200px]"
                        placeholder="请输入案件阶段"
                    />
                </el-form-item>
                <el-form-item label="国家(地区)" prop="nation">
                    <el-input
                        v-model="formData.nation"
                        class="w-[200px]"
                        placeholder="请输入国家(地区)"
                    />
                </el-form-item>
                <el-form-item label="申请类型" prop="applicationType">
                    <el-select
                        class="w-[200px]"
                        v-model="formData.applicationType"
                        placeholder="请选择申请类型"
                    >
                        <el-option label="发明" value="1" />
                        <el-option label="实用新型" value="2" />
                        <el-option label="外观设计" value="3" />
                    </el-select>
                </el-form-item>
                <el-form-item label="业务类型" prop="businessType">
                    <el-select
                        class="w-[200px]"
                        v-model="formData.businessType"
                        placeholder="请选择业务类型"
                    >
                        <el-option label="普通新申请" value="1" />
                        <el-option label="专利转让" value="2" />
                        <el-option label="无效案件" value="3" />
                    </el-select>
                </el-form-item>

                <el-form-item
                    label="内部加减顺序"
                    v-show="showEdit"
                    prop="interiorAddSubtractOrder"
                >
                    <el-input
                        class="w-[200px]"
                        v-model="formData.interiorAddSubtractOrder"
                        placeholder="请输入内部加减顺序"
                    />
                </el-form-item>
                <el-form-item
                    label="内部月数增量"
                    v-show="showEdit"
                    prop="interiorMonthlyIncrement"
                >
                    <el-input
                        class="w-[200px]"
                        v-model="formData.interiorMonthlyIncrement"
                        placeholder="请输入内部月数增量"
                    />
                </el-form-item>
                <el-form-item label="内部天数增量" v-show="showEdit" prop="interiorDaysIncrement">
                    <el-input
                        class="w-[200px]"
                        v-model="formData.interiorDaysIncrement"
                        placeholder="请输入内部天数增量"
                    />
                </el-form-item>
                <el-form-item label="内部基准日期" v-show="showEdit" prop="interiorBaseDate">
                    <!-- class="flex-1 !flex" -->
                    <el-date-picker
                        class="w-[200px]"
                        v-model="formData.interiorBaseDate"
                        type="datetime"
                        clearable
                        value-format="YYYY-MM-DD hh:mm:ss"
                        placeholder="请选择内部基准日期"
                    />
                </el-form-item>
                <el-form-item
                    label="客户加减顺序"
                    v-show="showEdit"
                    prop="customerAddSubtractOrder"
                >
                    <el-input
                        class="w-[200px]"
                        v-model="formData.customerAddSubtractOrder"
                        placeholder="请输入客户加减顺序"
                    />
                </el-form-item>
                <el-form-item
                    label="客户月数增量"
                    v-show="showEdit"
                    prop="customerMonthlyIncrement"
                >
                    <el-input
                        class="w-[200px]"
                        v-model="formData.customerMonthlyIncrement"
                        placeholder="请输入客户月数增量"
                    />
                </el-form-item>
                <el-form-item label="客户天数增量" v-show="showEdit" prop="customerDaysIncrement">
                    <el-input
                        class="w-[200px]"
                        v-model="formData.customerDaysIncrement"
                        placeholder="请输入客户天数增量"
                    />
                </el-form-item>
                <el-form-item label="客户基准日期" v-show="showEdit" prop="customerBaseDate">
                    <el-date-picker
                        class="w-[200px]"
                        v-model="formData.customerBaseDate"
                        type="datetime"
                        clearable
                        value-format="YYYY-MM-DD hh:mm:ss"
                        placeholder="请选择客户基准日期"
                    />
                </el-form-item>
                <el-form-item
                    label="官方加减顺序"
                    v-show="showEdit"
                    prop="officialAddSubtractOrder"
                >
                    <el-input
                        class="w-[200px]"
                        v-model="formData.officialAddSubtractOrder"
                        placeholder="请输入官方加减顺序"
                    />
                </el-form-item>
                <el-form-item
                    label="官方月数增量"
                    v-show="showEdit"
                    prop="officialMonthlyIncrement"
                >
                    <el-input
                        class="w-[200px]"
                        v-model="formData.officialMonthlyIncrement"
                        placeholder="请输入官方月数增量"
                    />
                </el-form-item>
                <el-form-item label="官方天数增量" v-show="showEdit" prop="officialDaysIncrement">
                    <el-input
                        class="w-[200px]"
                        v-model="formData.officialDaysIncrement"
                        placeholder="请输入官方天数增量"
                    />
                </el-form-item>
                <el-form-item label="官方基准日期" v-show="showEdit" prop="officialBaseDate">
                    <el-date-picker
                        class="w-[200px]"
                        v-model="formData.officialBaseDate"
                        type="datetime"
                        clearable
                        value-format="YYYY-MM-DD hh:mm:ss"
                        placeholder="请选择官方基准日期"
                    />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import {
    customertermruleEdit,
    customertermruleAdd,
    customertermruleDetail
} from '@/api/customer/customerTermRule'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const showEdit = ref(true)
const popupTitle = computed(() => {
    if (mode.value == 'edit') {
        showEdit.value = true
    }
    if (mode.value == 'add') {
        showEdit.value = false
    }
    return mode.value == 'edit' ? '编辑客户期限规则' : '新增客户期限规则'
})

const formData = reactive({
    id: '',
    caseTypeId: '',
    caseType: '',
    handlingInformation: '',
    caseStageId: '',
    caseStage: '',
    nation: '',
    applicationTypeId: '',
    applicationType: '',
    businessTypeId: '',
    businessType: '',
    interiorBaseDate: '',
    interiorAddSubtractOrder: '',
    interiorMonthlyIncrement: '',
    interiorDaysIncrement: '',
    customerBaseDate: '',
    customerAddSubtractOrder: '',
    customerMonthlyIncrement: '',
    customerDaysIncrement: '',
    officialBaseDate: '',
    officialAddSubtractOrder: '',
    officialMonthlyIncrement: '',
    officialDaysIncrement: ''
})

const formRules = {}

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    mode.value == 'edit' ? await customertermruleEdit(data) : await customertermruleAdd(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await customertermruleDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
